package 笔试;

import java.util.*;

/**
 * @author:谢君臣
 * @Date:2021/4/2319:21
 * @version:1.0
 * @function:
 */
public class T02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = Integer.parseInt(scanner.next());
        int[] arr = null;
        while (t > 0) {
            int n = Integer.parseInt(scanner.next());
            int k = Integer.parseInt(scanner.next());
            List<Node> list = putNode(n); // 只需要记录每个集合的第一个位置（1，3，5，7，9...）
            arr = changeIndex(list, n, k); // 链表循环,并给数组赋值
            System.out.println(Arrays.toString(arr));
            t--;
        }
    }

    public void solution(int n, int k) {
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < n; j++) {
                if (j % 2 == 0) { //只遍历0、2、4、6、8...
                    if (2 * j + 1 > n) {
                        continue;
                    }
                    int t = j;
                    int temp = arr[t];
                    while (2 * t + 1 < n) {
                        arr[t] = arr[2 * t + 1];
                        t = 2 * t + 1;
                    }
                    arr[t] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    private static int[] changeIndex(List<Node> list, int n, int k) {
        int[] arr = new int[n];
        for (int i = 1; i <= list.size(); i++) {
            int index = i * 2 - 1;
            Node node = list.get(i - 1);
            arr = AddValue(arr, node, k, index);
        }
        return arr;
    }

    private static int[] AddValue(int[] arr, Node node, int k, int index) {
        int size = node.size; // 记录当前连的长度
        k = k % size; // 计算有效的循环次数
        if (k > 0) { // 节点循环,尾插法
            k--;
            Node p = node;
            Node q = node.next;
            while (k > 0) {
                q = q.next;
                p = p.next;
                k--;
            }
            p.next = null;
            p = q;
            while (p.next != null) p = p.next;
            p.next = node;
            node = q;
        }
        while (index <= arr.length) {  // 给数组赋值，左子树下标为 2 * index -1
            int temp = node.i;
            arr[index - 1] = temp;
            node = node.next;
            index = 2 * index;
        }
        return arr;
    }

    private static List<Node> putNode(int L) {
        List<Node> list = new ArrayList<>();
        int i = 1;
        while (i <= L) {
            Node root = new Node(i, 1);
            Node node = root;
            int temp = i * 2;
            while (temp <= L) {
                Node n = new Node(temp);
                node.next = n;
                node = node.next;
                temp = temp * 2;
                root.size = ++root.size;
            }
            list.add(root);
            i = i + 2;
        }
        return list;
    }

}

class Node {
    int i;
    int size;
    Node next;

    public Node() {
    }

    public Node(int i) {
        this.i = i;
    }

    public Node(int i, int size) {
        this.i = i;
        this.size = size;
    }
}